package com.example.test_activity.fragment;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.test_activity.R;
import com.example.test_activity.http.HttpUtil;
import com.example.test_activity.utils.GetCourse;
import com.example.test_activity.utils.NoDoubleClickListener;
import com.example.test_activity.viewUtil.CoordinatorMenu;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

public class CourseFragment extends Fragment {

    GetCourse getCourse = new GetCourse();

    private View view;
    private TextView textView;
    private String error;
    private TextView title;
    private ImageView head;
    private CoordinatorMenu menu;
    private ImageView refresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.view = inflater.inflate(R.layout.fragment_course,container,false);

        init();
        title.setText("选课");

        getCourse();

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        head.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (menu.isOpened())
                {
                    menu.closeMenu();
                }else {
                    menu.openMenu();
                }
            }
        });

        refresh.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                getCourse();
            }
        });
    }

    private void init() {
        head = (ImageView) view.findViewById(R.id.iv_head);
        textView = (TextView) view.findViewById(R.id.course);
        title = (TextView) view.findViewById(R.id.main_title);
        menu = (CoordinatorMenu) getActivity().findViewById(R.id.menu);
        refresh = (ImageView) view.findViewById(R.id.top_refresh);
    }


    private void getCourse()
    {
        HttpUtil.getInstance().sendGetPost().getCourse()
                .map(new Function<ResponseBody, ResponseBody>() {
                    @Override
                    public ResponseBody apply(@NonNull ResponseBody responseBody) throws Exception {
                        error = getCourse.getUrl_4(responseBody.string());
                        return responseBody;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseBody responseBody) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        textView.setTextColor(getResources().getColor(R.color.colorRed));
                        textView.setText(error);
                    }
                });
    }
}
